package cerebrum.controller;

import java.io.*;
import cerebrum.listener.DownloadFinishListener;
import cerebrum.listener.RequestFinishListener;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtil {
    /**
     * 获取图片地址
     *
     * @param address
     * @param listener
     */
    public static void doGet(String address, RequestFinishListener listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection conn = null;
                InputStream in = null;
                BufferedReader reader = null;
                StringBuffer result = new StringBuffer();
                try {
                    URL url = new URL(address);
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setReadTimeout(5000);
                    conn.setConnectTimeout(3000);
                    conn.connect();
                    in = conn.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(in));
                    String line = "";
                    while ((line = reader.readLine()) != null) {
                        result.append(line);
                    }
                    if (listener != null) {
                        listener.finish(result.toString());
                    }
                    reader.close();
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    listener.error(e);
                } finally {
                    if (conn != null) {
                        conn.disconnect();
                    }
                }

            }
        }).start();
    }

    /**
     * 下载图片
     *
     * @param address 图片网址
     * @param dir 保存路径
     * @param name 文件名
     * @throws IOException
     */
    public static void download(String address, String dir, String name, DownloadFinishListener listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection conn = null;
                FileOutputStream out = null;
                InputStream in = null;
                File saveFile = null;
                try {
                    saveFile = new File(dir + "\\" + name);
                    if (saveFile.exists()) {
                        saveFile.delete();
                    }
                    System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2,SSLv3");
                    out = new FileOutputStream(saveFile);
                    URL url = new URL(address);
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setReadTimeout(50000);
                    conn.setConnectTimeout(50000);
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)");
                    conn.connect();
                    in = conn.getInputStream();
                    int len = -1;
                    byte[] buffer = new byte[1024];
                    while ((len = in.read(buffer)) != -1) {
                        out.write(buffer, 0, len);
                    }
                    listener.finish(address);
                    out.close();
                    in.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    // 删除未正确下载的图片
                    if (saveFile != null) {
                        saveFile.delete();
                    }
                    listener.error(ex,address);
                } finally {
                    if (conn != null) {
                        conn.disconnect();
                    }
                }
            }
        }).start();
    }
}
